#include "rightpanel.h"
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QDebug>
#include <QListWidgetItem>
#include <QJsonObject>
#include <QVariant>
#include <QLabel>

RightPanel::RightPanel(QWidget *parent) :
    QFrame(parent),
    mTypeListWidget(new QListWidget(this)),
    mEntityListWidget(new EntityListWidget(this))
{
    setObjectName("RightPanel");
    initUI();
}

void RightPanel::initUI()
{
    QVBoxLayout* layout = new QVBoxLayout;
    layout->setContentsMargins(0, 0, 0, 0);

    mTypeListWidget->setObjectName("TypeListWidget");
    mTypeListWidget->setGridSize(QSize(80, 30));
    mTypeListWidget->setFlow(QListWidget::LeftToRight);
    mTypeListWidget->setItemAlignment(Qt::AlignCenter);
//    mTypeListWidget->setResizeMode(QListWidget::Adjust);
    mTypeListWidget->setViewMode(QListWidget::IconMode);
    mTypeListWidget->setMovement(QListWidget::Static);
    mTypeListWidget->setWrapping(false);
    QStringList typeList;
    typeList << "Elements"
             << "Logic chips"
             << "Others";
    mTypeListWidget->addItems(typeList);


    mEntityListWidget->setObjectName("EntityListWidget");
    mEntityListWidget->setGridSize(QSize(110, 100));
    mEntityListWidget->setFlow(QListWidget::LeftToRight);
    mEntityListWidget->setWrapping(true);
    mEntityListWidget->setItemAlignment(Qt::AlignCenter);
    mEntityListWidget->setResizeMode(QListWidget::Adjust);
    mEntityListWidget->setViewMode(QListWidget::IconMode);
    mEntityListWidget->setMovement(QListWidget::Static);
    QStringList entityList;
    entityList << "Ground"
               << "WhiteLed"
               << "DT2821B"
               << "DT2821A"
               << "74Ls138"
               << "74LCX157-D";
    foreach (const QString& name, entityList) {
        QListWidgetItem* item = new QListWidgetItem(mEntityListWidget);
        item->setText(name);
        QJsonObject obj;
        obj.insert("EntityName", name);
        item->setData(Qt::EditRole, QVariant(obj));
        mEntityListWidget->addItem(item);
        QLabel* label = new QLabel(this);
        label->setText(name);
        label->setObjectName("Entity");
        label->setAlignment(Qt::AlignCenter);
        mEntityListWidget->setItemWidget(item, label);
    }
    mTypeListWidget->setContentsMargins(0, 0, 0, 0);
    mTypeListWidget->setFixedHeight(50);
    layout->addWidget(mTypeListWidget);
    layout->addWidget(mEntityListWidget);

    setLayout(layout);
}

void RightPanel::initConnect()
{

}

void RightPanel::mousePressEvent(QMouseEvent *e)
{
    qDebug() << e;
    QFrame::mousePressEvent(e);
}

EntityTypeButton::EntityTypeButton(QWidget *parent): QPushButton(parent)
{
    setObjectName("EntityType");
}

EntityTypeButton::EntityTypeButton(const QString &text, QWidget *parent): QPushButton(text, parent)
{
    setObjectName("EntityType");
}

EntityListWidget::EntityListWidget(QWidget *parent):
    QListWidget(parent),
    mItemDragging(false)
{

}

void EntityListWidget::mousePressEvent(QMouseEvent *event)
{
    mDragItem = itemAt(event->pos());
    mLastPressPos = event->pos();
    QListWidget::mousePressEvent(event);
}

void EntityListWidget::mouseMoveEvent(QMouseEvent *e)
{
    if((e->pos() - mLastPressPos).manhattanLength() > 10 && !mItemDragging && mDragItem) {
        mItemDragging = true;

        QDrag* drag = new QDrag(this);
        QMimeData *mimeData = new QMimeData;

        QJsonObject obj = mDragItem->data(Qt::EditRole).toJsonObject();
        const QString& entityName = obj.value("EntityName").toString();
        mimeData->setText(entityName);
        drag->setMimeData(mimeData);
        drag->exec();
        mItemDragging = false;
    }
    QListWidget::mouseMoveEvent(e);
}
